<?php 
	include '../init.php';
	include ADMIN_PATH.'/header.php';

	//遍历商品
	$model=new Model('goods');

	$str1='';
	$where=[];
	$url='';
	//搜索商品名称
	if (!empty($_GET['name'])) 
	{
		//接收名称
		$name=$_GET['name'];
		//sql条件
		$where['name']=$name;
		//拼接url
		$url.='&name='.$name;
	}
	//搜索新品
	if (!empty($_GET['is_new'])) 
	{
		//拼接条件
		$where['is_new']=$_GET['is_new'];
		//拼接url
		$url.='&is_new='.$_GET['is_new'];
	}
	//拼接sql语句中的字符串
	if (!empty($where)) 
	{
		foreach ($where as $key => $value) 
		{
			if ($key=='name') 
			{
				$str1.="and g.".$key." like '%".$value."%'";
				//跳出当前循环进入下次循环
				continue;
			}
			$str1.='and g.'.$key.'='.$value;
		}
	}
	//每页出现几条
	$num=2;
	//总条数
	$sqlc="select
				count(*)
			from
				goods      g,
				goods_img  gi,
				category   c
			where
				g.cate_id=c.id
			and
				g.id=gi.goods_id
			and
				gi.is_cover=1
			$str1
				";
	$total=$model->find($sqlc)[0]['count(*)'];
	//总页数
	$pages=ceil($total/$num);
	//当前页
	$p=$_GET['p']?$_GET['p']:1;
	$p=min($p,$pages);
	$p=max($p,1);

	//开始
	$start=($p-1)*$num;
	//偏移量
	$offset=$num;
	//下一页
	$next=$p+1;
	//前一页
	$prev=$p-1;

	//sql语句
	$sql="select
			g.id   	   gid,
			g.name     gname,
			g.is_new   gnew,
			g.is_hot   ghot,
			g.is_bast  gb,
			g.price    gp,
			g.stock    gs,
			gi.name    giname,
			c.name     cname,
			g.addtime  gtime
			from
				goods g,
				goods_img gi,
				category c
			where 
				g.id=gi.goods_id
			and
				c.id=g.cate_id
			and
				gi.is_cover=1
			$str1

			limit $start,$offset
			";
	$list=$model->find($sql);

	if (!empty($list)) {	
		foreach ($list as $k => $v) 
		{
			$list[$k]['giname']=URL.'/public/uploads/'.substr($v['giname'],0,4).'/'.substr($v['giname'], 4,2).'/'.substr($v['giname'], 6,2).'/80_'.$v['giname'];
			$list[$k]['gtime']=date('Y年m月d日',$v['gtime']);
		}
	}

	$str="
	<a href='".ADMIN_URL."/goods/index.php'>首页</a>
	<a href='".ADMIN_URL."/goods/index.php?p=$prev'>上一页</a>
	<a href='".ADMIN_URL."/goods/index.php?p=$p'>$p</a>
	<a href='".ADMIN_URL."/goods/index.php?p=$next'>下一页</a>
	<a href='".ADMIN_URL."/goods/index.php?p=$pages'>尾页</a>
	";
?>
<div class="m_right">
	<div class="m_main">
		<h1 style="font:bold 26px/40px 宋体;">这是商品管理页面</h1>
		<a href="<?php echo ADMIN_URL;?>/goods/add.php"><button style="width:100px;height:40px;border:none;background:#2CAF01;border-radius:10px;color:#fff;">添加商品</button></a>

		<table class="table" border="1" cellpadding="5" cellspacing="0">
			<tr height="50">
				<th colspan="11">
					<form action="">
						<input type="text" name="name" />
						<label>
						<input type="radio" name="is_new" value="1" style="display:block;float:left;width:50px;" /><span style="display:block;float:left;line-height:35px;">新品</span>
						</label>
						<button type="submit">搜索</button>
					</form>
				</th>
			</tr>
			<tr bgcolor="#ccc" height="50">
				<th>编号</th>
				<th>商品名称</th>
				<th>图片</th>
				<th>价格</th>
				<th>分类</th>
				<th>库存</th>
				<th>新品</th>
				<th>上架</th>
				<th>热销</th>
				<th>时间</th>
				<th>操作</th>
			</tr>
			<?php foreach ($list as $key => $value) :?>
				<tr>
					<td><?php echo $value['gid'];?></td>
					<td><?php echo $value['gname'];?></td>
					<td><img src="<?php echo $value['giname'];?>" alt=""></td>
					<td>$:<?php echo $value['gp'];?></td>
					<td><?php echo $value['cname'];?></td>
					<td><?php echo $value['gs'];?></td>
					<td>
						<?php 
						echo  $value['gnew']?'<a href="'.ADMIN_URL.'/goods/action.php?a=new&p='.$p.'&id='.$value['gid'].'"><img style="display:block;border-radius:40px;" src="'.URL.'/public/images/dui.png"/></a>':'<a href="'.ADMIN_URL.'/goods/action.php?a=new&p='.$p.'&id='.$value['gid'].'"><img style="display:block;border-radius:40px;" src="'.URL.'/public/images/x.png"></a>';
						?>
					</td>
					<td>
						<?php 
						echo $value['gb']?'<a href="'.ADMIN_URL.'/goods/action.php?a=bast&p='.$p.'&id='.$value['gid'].'"><img src="'.URL.'/public/images/dui.png" style="display:block;border-radius:40px;" alt="图片加载失败"></a>':'<a href="'.ADMIN_URL.'/goods/action.php?a=bast&p='.$p.'&id='.$value['gid'].'"><img style="display:block;border-radius:40px;" src="'.URL.'/public/images/x.png"><a>';
						?>
					</td>
					<td>
						<?php 
						echo $value['ghot']?'<a href="'.ADMIN_URL.'/goods/action.php?a=hot&p='.$p.'&id='.$value['gid'].'"><img style="display:block;border-radius:40px;" src="'.URL.'/public/images/dui.png" alt="图片加载失败"></a>':'<a href="'.ADMIN_URL.'/goods/action.php?a=hot&p='.$p.'&id='.$value['gid'].'"><img style="display:block;border-radius:40px;" src="'.URL.'/public/images/x.png"><a>';
						?>
					</td>
					<td><?php echo $value['gtime'];?></td>
					<td><a href="<?php echo ADMIN_URL?>/goods/detail.php?id=<?php echo $value['gid'];?>">查看详情</a><br/><a href="<?php echo ADMIN_URL;?>/goods/action.php?a=delete&id=<?php echo $value[gid];?>">删除</a></td>
				</tr>
			<?php endforeach;?>
			<tr height="50">
				<td colspan="11"><?php echo $str;?></td>
			</tr>
		</table>
	</div>
</div>
<?php
	include ADMIN_PATH.'/footer.html';
?>